#include "HookLibrary.h"
#include "MessageWindow.h"

#define BGSHELL_SECTION_NAME (L"Local\\BGShell-KeyboardHook-Section")

static volatile HWND g_hWnd = NULL;

HWND WINAPI FindMessageWindow();

HWND WINAPI MsgWindow()
{
	if (g_hWnd == NULL)
	{
		HWND hWnd = FindMessageWindow();

		if (IsWindow(hWnd))
		{
			g_hWnd = hWnd;
		}
	}

	return g_hWnd;
}

HWND WINAPI FindMessageWindow()
{
	HWND    hWnd         = NULL;
	HANDLE  hFileMapping = OpenFileMappingW(FILE_MAP_READ, FALSE, BGSHELL_SECTION_NAME);
	
	if (hFileMapping != NULL)
	{
		CONST HWND * pSection = MapViewOfFile(
			hFileMapping, 
			FILE_MAP_READ, 
			0, 0,
			sizeof(HWND));

		if (pSection != NULL)
		{
			hWnd = *pSection;
			UnmapViewOfFile(pSection);
		}

		CloseHandle(hFileMapping);
	}

	return hWnd;
}
